#include "pthreadpool.h"
typedef struct train_s
{
    int length;
    char data[1000];
}train_t;
//v.3.1
//int transfile(int netfd)
//{
//    train_t train;
//    char filename[]="file1";
//    train.length=strlen(filename);
//    memcpy(train.data,filename,train.length);
//    send(netfd,&train,sizeof(int)+train.length,0);
//    int fd=open(filename,O_RDWR);
//    while(1)
//    {
//        bzero(&train,sizeof(train));
//        ssize_t sret=read(fd,train.data,sizeof(train.data));
//        if(sret<=0)break;
//        train.length=sret;
//        send(netfd,&train,sizeof(int)+train.length,0);
//    }
//    train.length=0;
//    send(netfd,&train,sizeof(int),0);
//    close(fd);
//    return 0;
//} 
//int transfile(int netfd)
//{
//    train_t train;
//    char filename[]="file1";
//    train.length=strlen(filename);
//    memcpy(train.data,filename,train.length);
//    send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
//    int fd=open(filename,O_RDWR);
//    struct stat statbuf;
//    fstat(fd,&statbuf);
//    off_t filesize = statbuf.st_size;
//    printf("filesize=%ld\n",filesize);
//    train.length=sizeof(filesize);
//    memcpy(train.data,&filesize,train.length);
//    send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
//    while(1)
//    {
//        bzero(&train,sizeof(train));
//        ssize_t sret=read(fd,train.data,sizeof(train.data));
//        if(sret<=0)break;
//        train.length=sret;
//        send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
//    }
//    train.length=0;
//    send(netfd,&train,sizeof(int),0);
//    close(fd);
//    return 0;
//}
//v4.1
//int transfile(int netfd)
//{
//    train_t train;
//    char filename[]="file1";
//    train.length=strlen(filename);
//    memcpy(train.data,filename,train.length);
//    send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
//    int fd=open(filename,O_RDWR);
//    struct stat statbuf;
//    fstat(fd,&statbuf);
//    off_t filesize = statbuf.st_size;
//    printf("filesize=%ld\n",filesize);
//    train.length=sizeof(filesize);
//    memcpy(train.data,&filesize,train.length);
//    send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
//    char *p=(char*)mmap(NULL,filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
//    off_t sendsize=0;
//    while(sendsize<filesize)
//    {
//        if(filesize-sendsize>=1000)
//        {
//            train.length=1000;
//        }
//        else
//        {
//            train.length=filesize-sendsize;
//        } 
//        send(netfd,&train.length,sizeof(int),MSG_NOSIGNAL);
//        send(netfd,p+sendsize,train.length,MSG_NOSIGNAL);
//        sendsize+=train.length;
//    }
//    train.length=0;
//    send(netfd,&train,sizeof(int),0);
//    close(fd);
//    munmap(p,filesize);
//    return 0;
//}
//v5.0
//int transfile(int netfd)
//{
//    train_t train;
//    char filename[]="file1";
//    train.length=strlen(filename);
//    memcpy(train.data,filename,train.length);
//    send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
//    int fd=open(filename,O_RDWR);
//    struct stat statbuf;
//    fstat(fd,&statbuf);
//    off_t filesize = statbuf.st_size;
//    printf("filesize=%ld\n",filesize);
//    train.length=sizeof(filesize);
//    memcpy(train.data,&filesize,train.length);
//    send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
//    char *p=(char*)mmap(NULL,filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
//    send(netfd,p,filesize,MSG_NOSIGNAL);
//    close(fd);
//    munmap(p,filesize);
//    return 0;
//}
//v5.1
int transfile(int netfd,char *filename)
{
    train_t train;
    /* char filename[]="file1"; */
    train.length=strlen(filename);
    memcpy(train.data,filename,train.length);
    send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
    int fd=open(filename,O_RDWR);
    struct stat statbuf;
    fstat(fd,&statbuf);
    off_t filesize = statbuf.st_size;
    printf("filesize=%ld\n",filesize);
    train.length=sizeof(filesize);
    memcpy(train.data,&filesize,train.length);
    send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
    sendfile(netfd,fd,0,filesize);
    close(fd);
    return 0;
} 
int recvfile(int netfd)
{
    train_t train;
    char filename[4096]={0};
    recv(netfd,&train.length,sizeof(int),MSG_WAITALL );
    recv(netfd,train.data,train.length,MSG_WAITALL );
    memcpy(filename,train.data,train.length);
    int fd=open(filename,O_RDWR|O_CREAT|O_TRUNC,0666);
    while(1)
    {
        bzero(&train,sizeof(train));
        recv(netfd,&train.length,sizeof(int),MSG_WAITALL );
        if(train.length<=0)break;
        recv(netfd,&train.data,train.length,MSG_WAITALL );
        write(fd,train.data,train.length);
    }
    close(fd);
    return 0;
} 
